package com.usian.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.constants.user.UserConstants.AdminConstans;
import com.usian.model.article.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;

import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign.ArticleFeign;
import com.usian.user.feign.WmUserFeign.WemediaFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Override
    public PageResponseResult loadListByStatus(AuthDto dto) {
        //检测参数是否为空
        if (dto==null){
            return (PageResponseResult) ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        //创建条件构造器
        QueryWrapper<ApUserRealname> Wrapper = new QueryWrapper<>();
        if (dto.getStatus()!=null){
            Wrapper.like("status",dto.getStatus());
        }
        Page page = new Page(dto.getPage(), dto.getSize());
        IPage result=page(page,Wrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Autowired
    ArticleFeign articleFfeign;
    @Autowired
    WemediaFeign wemediaFeign;
    @Autowired
    ApUserMapper apUserMapper;

    @Override
    @Transactional
//    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, short status) {
        //1 检查参数
        if (dto == null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        if (statusCheck(status)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2 修改状态
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if (dto.getMsg()!=null){
            apUserRealname.setReason(dto.getMsg());
        }
        updateById(apUserRealname);
        //3 认证通过创建自媒体账号和作者账号
        if (status==AdminConstans.PASS_AUTH){
            ResponseResult responseResult = CreatMediaAndAuth(dto);
            if (responseResult !=null){
                return responseResult;
            }
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 创建自媒体账号和作者账号
     * @param dto
     * @return
     */
    public ResponseResult CreatMediaAndAuth(AuthDto dto){
        //1 通过认证id 获取认证列表用户信息
        ApUserRealname byId = getById(dto.getId());
        //2 通过认证列表用户id 获取用户信息
        ApUser apUser = apUserMapper.selectById(byId.getUserId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NULL_CANSHU);
        }
        //3 通过用户Name 获取自媒体人信息
        WmUser wmUser = wemediaFeign.findByname(apUser.getName());
        //4 判断自媒体用户是否为空 为空创建新自媒体账号
        if (wmUser == null || wmUser.getId() ==null){
            wmUser = new WmUser();
            wmUser.setApUserId(apUser.getId());
            wmUser.setName(apUser.getName());
            wmUser.setPassword(apUser.getPassword());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setStatus(9);
            wmUser.setCreatedTime(new Date());
            wemediaFeign.save(wmUser);
        }
        //5 创建作者账号
            creatAuth(wmUser);
        //6 修改用户身份标记
        // 0 普通用户 1 自媒体用户 2 网络大v
        short a=1;
        apUser.setFlag(a);
        apUserMapper.updateById(apUser);
        return null;
    }

    public void creatAuth(WmUser wmUser){
        Integer apUserId = wmUser.getApUserId();
        ApAuthor apAuthor = articleFfeign.findByUserId(apUserId);
        if (apAuthor==null){
            apAuthor = new ApAuthor();
            apAuthor.setUserId(wmUser.getApUserId());
            apAuthor.setCreatedTime(new Date());
            apAuthor.setName(wmUser.getName());
            apAuthor.setType(AdminConstans.AUTHOR_TYPE);
            articleFfeign.save(apAuthor);
        }
    }

    /**
     * 检查status参数是否规范
     * @param status
     * @return
     */
    public boolean statusCheck(Short status){
        if (status == null ||(!status.equals(AdminConstans.PASS_AUTH) && !status.equals(AdminConstans.FILE_AUTH))){
            return true;
        }
        return false;
    }


}
